//
var seles = []; //0:list 1:info
function active(fnode, rnode, infoobj) {
    if (fnode.hasAttribute('checked')) fnode.removeAttribute('checked');
    rnode.setAttribute('checked', 'checked');
    var desid = rnode.getAttribute('desid');
    loader.loadedPages[desid].display(infoobj);
}

function loadpage(id) {
    var node = document.getElementById(id);
    loader.loadToNode(id, node, id + '.html', false); //sync load
    loader.loadedPages[id].init(funs);
    //init fun
}
var funs = {};
var hisarr = []; //存储 {sign:sign,node:node}
function hispop(index) {
    var info = hisarr[index];
    hisarr.remove(index);
    info.parentNode.removeChild(info.node);
}

function hisremove(sign) {
    for (var t = 0; t < hisarr.length; ++t) {
        var obj = hisarr[t];
        if (obj.sign == sign) {
            hisarr.splice(t,1);
            obj.node.parentNode.removeChild(obj.node);
            break;
        }
    }
}

function hisclick(node) {
    var sign = null;
    for (var t = 0; t < hisarr.length; ++t) {
        var item = hisarr[t];
        if (item.node == node) {
            funs.displayInfo(item.sign);
            return;
        }
    }
}

function hisclear() {
    var s = document.getElementById("history_bar");
    var list = s.childNodes;
    while (list.length != 0) s.removeChild(list[0]);
    hisarr = [];
    delCookie("uptool_history");
}

function hispush(sign) {
    if (sign == null || sign == undefined) return;
    hisremove(sign);
    if (hisarr.length >= 5) {
        hispop(0);
    }
    var data = DataInterface.CompetFound(sign);
    if(data==null) return;
    //view operate
    var text = document.getElementById('history').innerHTML;
    var html = text.format(data.name); //模板替换
    var node = GTlate.GenTools.GetNodeFromText(html);
    var obj = {
        sign: sign
        , node: node
    };
    var fobj = hisarr[hisarr.length - 1];
    hisarr.push(obj);
    var bar = document.getElementById("history_bar");
    if (fobj == undefined) bar.appendChild(node);
    else bar.insertBefore(node, fobj.node);
    //写入cookie
    var ckobj = [];
    for (var t = 0; t < hisarr.length; ++t) {
        var tobj = hisarr[t];
        ckobj.push(tobj.sign);
    }
    setCookie("uptool_history", JSON.stringify(ckobj));
}

function hisread() {
    //从cookie中读取历史
    var tarr = JSON.parse(getCookie("uptool_history"));
    if (tarr == null || tarr == undefined) return;
    for (var t = 0; t < tarr.length; ++t) {
        hispush(tarr[t]);
    }
}
funs.displayInfo = function (sign) {
    //from page
    var data = DataInterface.CompetFound(sign);
    if (data == null) {
        alert("没有找到指定项目!");
        funs.backToList();
        return;
    }
    hispush(sign);
    active(seles[0], seles[1], DataInterface.CompetFound(sign));
}
funs.backToList = function () {
    active(seles[1], seles[0], listobj);
}
var listobj = null;
addinit(function () {

    // var hisbtn=document.getElementById('hisbutton');
    // hisbtn.onclick=function () {
    //     this.addClass('active');
    // };
    var his=document.getElementById('his');
    var hisbutton=document.getElementById('hisbutton');
    his.onclick=function(e){
        if(e.target==this||e.target==hisbutton)
            addclass(this,'active');
    }
    his.onmouseleave=function (e) {
        if(e.target==this)
            this.removeClass('active');
    }
    if (!DataInterface.isok()) return;
    seles.push(document.getElementById('selelist'));
    seles.push(document.getElementById('seleinfo'));
    listobj = DataInterface.GetCompetList();
    loadpage('list');
    loadpage('info');
    hisread();
    var sign = DataInterface.GetPageLSign();
    if (sign == "") {
        funs.backToList();
    }
    else {
        funs.displayInfo(sign);
    }
    document.getElementById('listbtn').onclick = function () {
        active(seles[1], seles[0], listobj);
    };
});

// function timetrans(str) {
//     //xxxxTxxxx.000Z
//     var tfun = function (str) {
//         var ret;
//         ret = str.replace("T", " ");
//         ret = str.replace(".000Z", "");
//         var ss = new Date(Date.parse(ret));
//         return ss.Format("yyyy-MM-dd  hh:mm:ss");
//     };
//     return tfun(str);
// }